package com.im.base;

import com.only.query.page.AbstractPage;
/**
 * 分页
 * @author XiaHui
 * @date 2015年12月25日 下午11:42:57
 * @version 0.0.1
 */
public class PageImpl extends AbstractPage {
	
	private int pageSize = 30;// 页码大小
	private int startResult = 0;// 起始记录数
	private int endResult = 0;// 起始记录数
	private int totalCount = 0;
	private int pageNumber = 1;
	private int totalPage;

	public int getTotalCount() {
		return totalCount;
	}

	@Override
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	@Override
	public int getPageSize() {
		if (pageSize < 0) {
			return 10;
		}
		return pageSize;
	}

	@Override
	public void setPageSize(int pageSize) {
		if (pageSize > 0) {
			this.pageSize = pageSize;
		}
	}

	public void setStartResult(int startResult) {
		this.startResult = startResult;
	}

	@Override
	public int getTotalPage() {
		if (this.getTotalCount() > 0) {
			totalPage = this.getTotalCount() / this.getPageSize();
			if (this.getTotalCount() % this.getPageSize() > 0) {
				totalPage = totalPage + 1;
			}
		}
		return totalPage;
	}

	@Override
	public int getStartResult() {
		if (startResult <= 0) {
			if(this.getPageNumber()>1){
				startResult = (this.getPageNumber() - 1) * this.getPageSize();
				if (startResult < 0) {
					startResult = 0;
				}
			}
		}
		return startResult;
	}

	@Override
	public void setEndResult(int endResult) {
		this.endResult = endResult;
	}

	@Override
	public int getEndResult() {
		endResult = (this.getPageNumber() * this.getPageSize());
		if (endResult > this.getTotalCount()) {
			endResult = this.getTotalCount();
		}
		return endResult;
	}

	@Override
	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	@Override
	public int getPageNumber() {
		return pageNumber;
	}

	public String getPageMenu() {
		int up = pageNumber - 1;
		int down = pageNumber + 1;
		if (up < 1) {
			up = 1;
		}
		if (down > getTotalPage()) {
			down = getTotalPage();
		}
		String pageMenu = "<div class=\"dataTables_info\" >显示第" + this.getStartResult() + "条记录-第" + this.getEndResult() + "条记录 ，一共" + this.getTotalCount() + "个记录</div>";
		pageMenu += "<div class=\"dataTables_paginate paging_simple_numbers\" >";
		if (pageNumber > 1) {
			pageMenu += "<a onclick=\"pagesChange($(this), 1);\" class=\"paginate_button previous disabled\" href=\"javascript:void(0)\" >首页</a>";
			pageMenu += "<a onclick=\"pagesChange($(this), " + up + ");\" class=\"paginate_button previous disabled\" href=\"javascript:void(0)\" >上一页</a>";
		}
		pageMenu += "	<span>";
		int temp;
		temp = pageNumber - 5;
		for (int i = 1; i < 11; i++) {
			if ((temp + i) > 0 && (temp + i) <= getTotalPage()) {
				if ((temp + i) == pageNumber) {
					pageMenu += "<a onclick=\"pagesChange($(this), " + (temp + i) + ");\" class=\"paginate_button current\" href=\"javascript:void(0)\" >" + (temp + i) + "</a>";
				} else {
					pageMenu += "<a onclick=\"pagesChange($(this), " + (temp + i) + ");\" class=\"paginate_button\" href=\"javascript:void(0)\" >" + (temp + i) + "</a>";
				}
			}
		}
		pageMenu += "	</span>";
		if (pageNumber <getTotalPage()) {
			pageMenu += "	<a onclick=\"pagesChange($(this), " + down + ");\" class=\"paginate_button next\" href=\"javascript:void(0)\" >下一页</a>";
			pageMenu += "	<a onclick=\"pagesChange($(this), " + getTotalPage() + ");\" class=\"paginate_button next\" href=\"javascript:void(0)\" >末页</a>";
		}
		pageMenu += "跳到<input id=\"goPage\" name=\"goPage\" type=\"text\" style=\"width:30px;height:22px;\" >页 ";
		pageMenu += "<button onclick=\"pagesChangeTo($(this), " + getTotalPage() + " );\"  value=\"确定\" type=\"button\" class=\"btn btn-success active\">确定</button>";
		pageMenu += "</div>";

		pageMenu += ("<input type=hidden  value=" + pageNumber + " name=\"page.pageNumber\" id=\"pages_pageNum\"> ");
		pageMenu += ("<input type=hidden  value=" + pageSize + " name=\"page.pageSize\"> ");
		return pageMenu;
	}
}
